package com.javacv.plus.examples.controller;

import com.javacv.plus.examples.service.VideoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "视频处理", description = "视频处理相关接口")
@RestController
@RequestMapping("/videos")
@RequiredArgsConstructor
public class VideoController {

    private final VideoService videoService;

    @Operation(summary = "转换视频格式")
    @PostMapping(value = "/convert", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String convertVideo(
        @Parameter(description = "视频文件") @RequestParam("file") MultipartFile file,
        @Parameter(description = "目标格式") @RequestParam("format") String format
    ) {
        return videoService.convertVideo(file, format);
    }

    @Operation(summary = "压缩视频")
    @PostMapping(value = "/compress", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String compressVideo(
        @Parameter(description = "视频文件") @RequestParam("file") MultipartFile file,
        @Parameter(description = "压缩质量") @RequestParam("quality") double quality
    ) {
        return videoService.compressVideo(file, quality);
    }

    @Operation(summary = "提取视频帧")
    @PostMapping(value = "/extract-frames", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String extractFrames(
        @Parameter(description = "视频文件") @RequestParam("file") MultipartFile file,
        @Parameter(description = "帧间隔") @RequestParam("interval") int interval
    ) {
        return videoService.extractFrames(file, interval);
    }

    @Operation(summary = "合并视频")
    @PostMapping(value = "/merge", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String mergeVideos(
        @Parameter(description = "视频文件列表") @RequestParam("files") MultipartFile[] files
    ) {
        return videoService.mergeVideos(files);
    }

    @Operation(summary = "添加水印")
    @PostMapping(value = "/watermark", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String addWatermark(
        @Parameter(description = "视频文件") @RequestParam("file") MultipartFile file,
        @Parameter(description = "水印图像") @RequestParam("watermark") MultipartFile watermark,
        @Parameter(description = "水印位置") @RequestParam("position") String position
    ) {
        return videoService.addWatermark(file, watermark, position);
    }

    @Operation(summary = "剪切视频")
    @PostMapping(value = "/cut", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String cutVideo(
        @Parameter(description = "视频文件") @RequestParam("file") MultipartFile file,
        @Parameter(description = "开始时间（秒）") @RequestParam("startTime") int startTime,
        @Parameter(description = "持续时间（秒）") @RequestParam("duration") int duration
    ) {
        return videoService.cutVideo(file, startTime, duration);
    }

    @Operation(summary = "旋转视频")
    @PostMapping(value = "/rotate", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String rotateVideo(
        @Parameter(description = "视频文件") @RequestParam("file") MultipartFile file,
        @Parameter(description = "旋转角度") @RequestParam("angle") int angle
    ) {
        return videoService.rotateVideo(file, angle);
    }

    @Operation(summary = "调整视频分辨率")
    @PostMapping(value = "/resize", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String resizeVideo(
        @Parameter(description = "视频文件") @RequestParam("file") MultipartFile file,
        @Parameter(description = "宽度") @RequestParam("width") int width,
        @Parameter(description = "高度") @RequestParam("height") int height
    ) {
        return videoService.resizeVideo(file, width, height);
    }
} 